package com.example.demo.controller;

import com.example.demo.model.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
@Controller
public class ListController {

    @Autowired
    StudentService studentService;
    @RequestMapping("list")
    public String get_list(ModelMap map,Student student){
        map.put("students",studentService.list(student));
        return "list";
    }
    @RequestMapping("list/delete/{id}")
    public String delete(@PathVariable Long id){
        studentService.deleteById(id);
        return "redirect:/list";
    }
    @RequestMapping("student/create")
    public String create(){
        return "create";
    }
    @RequestMapping("student/save")
    public String save(@RequestParam String age, @RequestParam String name){
        Student st = new Student(name,Integer.parseInt(age));
        studentService.save(st);
        return "redirect:/list";
    }
    @RequestMapping("list/update/{id}")
    public String update (ModelMap map,@PathVariable Long id){

        map.put("student",studentService.getbyid(id));
        return "update";
    }
    @RequestMapping("/list/edit")
    public String edit(Student st){
        studentService.save(st);
        return "redirect:/list";
    }
}
